package com.yundasys.lab13mybatisplus.algorithm;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.util.Collection;
import java.util.Date;
import java.util.Iterator;

/**
 * @PACKAGE_NAME: com.yundasys.lab13mybatisplus.algorithm
 * @NAME: OrderShardingAlgorithm
 * @USER: jiangta
 * @DATE: 2022/2/21
 * @TIME: 4:19 下午
 * @PROJECT_NAME: springboot-labs
 */
public class OrderCrtTimeShardingAlgorithm implements PreciseShardingAlgorithm<Date> {

    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<Date> preciseShardingValue) {
        System.out.println(collection);
        Date value = preciseShardingValue.getValue();
        Iterator<String> realTableNameIte = collection.iterator();
        while(realTableNameIte.hasNext()) {
            String next = realTableNameIte.next();
            if(next.endsWith(DateFormatUtils.format(value, "MM"))) {
                return next;
            }
        }
        return null;
    }
}
